package com.rock.cms.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.rock.cms.model.TApBanner;
import com.rock.cms.model.TCmArticle;
import com.rock.cms.model.TCmArticleCategory;

public class CmsUtil {

	//复制文章分类及文章
	public static void copyCategoryAndArticle(String srcAppId, String appId, String tenantId) {


		//目前仅支持同一篇文章属于唯一一个分类的场景，不支持多对多关系，将来改造时需要用到mapping表
		//移除已有数据
		List<TCmArticleCategory> lstOld = TCmArticleCategory.dao.find("select * from t_cm_article_category where app_id=?",appId);
		for(TCmArticleCategory item : lstOld) {
			//先移除文章分类下的文章
			batchDeleteByCategoryId(item.getId());
			//再移除文章分类
			TCmArticleCategory.dao.deleteById(item.getId());
		}
		
		//复制新的数据-先处理顶级分类- pid==0
		List<TCmArticleCategory> lstP = TCmArticleCategory.dao.find("select * from t_cm_article_category where app_id=? and pid=?",srcAppId,0);
		//存储父id新老对应关系
		Map<Long, Long> pmap = new HashMap<Long, Long>();
		
		for(TCmArticleCategory item : lstP) {
			
			//原ID
			long oid = item.getId();
			//先查询原文章分类下所有文章
			List<TCmArticle> artLstOld = TCmArticle.dao.find("select * from t_cm_article where category_id=?",item.getId());
			
			//复制文章分类
			TCmArticleCategory newItem = item.remove("id").remove("create_user").remove("update_user").remove("update_time");
			newItem.setAppId(Integer.parseInt(appId));
			newItem.setTenantId(Integer.parseInt(tenantId));
			newItem.setCreateTime(new Date());
			newItem.save();
			
			//复制新文章分类下的文章
			copyArticlesByCategory(artLstOld,newItem);
			
			//存储父id对应关系
			pmap.put(oid, newItem.getId());
		}
	

		//复制新的数据-再处理二级分类- pid!=0
		List<TCmArticleCategory> lstS = TCmArticleCategory.dao.find("select * from t_cm_article_category where app_id=? and pid<>?",srcAppId,0);
		for(TCmArticleCategory item : lstS) {
			//先查询原文章分类下所有文章
			List<TCmArticle> artLstOld = TCmArticle.dao.find("select * from t_cm_article where category_id=?",item.getId());
			
			//复制文章分类
			TCmArticleCategory newItem = item.remove("id").remove("create_user").remove("update_user").remove("update_time");
			newItem.setAppId(Integer.parseInt(appId));
			newItem.setTenantId(Integer.parseInt(tenantId));
			newItem.setCreateTime(new Date());
			//设置父ID
			newItem.setPid(pmap.get(item.getPid()));
			newItem.save();
			
			//复制新文章分类下的文章
			copyArticlesByCategory(artLstOld,newItem);
		}
	
	}
	

	public static void batchDeleteByCategoryId(Long cid) {
		//根据文章分类ID移除其下所有文章
		List<TCmArticle> lstOld = TCmArticle.dao.find("select * from t_cm_article where category_id=?",cid);
		for(TCmArticle item : lstOld) {
			TCmArticle.dao.deleteById(item.getId());
		}
	}
	

	public static void copyArticlesByCategory(List<TCmArticle> artLst, TCmArticleCategory cg) {
		for(TCmArticle item : artLst) {
			//复制文章分类
			TCmArticle newItem = item.remove("id").remove("create_user").remove("update_user").remove("update_time");
			newItem.setAppId(cg.getAppId());
			newItem.setTenantId(cg.getTenantId());
			newItem.setCreateTime(new Date());
			//设置文章分类
			newItem.setCategoryId(cg.getId());
			newItem.save();
		}
	}
	

	public static void copyBannerData(String srcAppId, String appId, String tenantId,int articleId) {
		
		//移除已有数据
		List<TApBanner> lstOld = TApBanner.dao.find("select * from t_ap_banner where app_id=?",appId);
		for(TApBanner item : lstOld) {
			TApBanner.dao.deleteById(item.getId());
		}
		
		//复制新的数据
		List<TApBanner> lst = TApBanner.dao.find("select * from t_ap_banner where app_id=?",srcAppId);
		for(TApBanner item : lst) {
			TApBanner newItem = item.remove("id").remove("create_user").remove("update_user").remove("update_time");
			newItem.setAppId(Integer.parseInt(appId));
			newItem.setTenantId(Integer.parseInt(tenantId));
			newItem.setCreateTime(new Date());
			newItem.setArticleId(articleId);
			newItem.save();
		}
	}
}
